import axios from 'axios'
import {baseUrl} from "@/api/baseUrl";
import store from "@/store/index"
import router from "@/router";
import {Message} from "element-ui";
const service=axios.create({
    baseURL:baseUrl,
    timeout:50000
})

service.interceptors.request.use(config=>{
    if (store.getters["user/getToken"]) {
        config.headers['TOKEN'] = store.getters["user/getToken"]
    }
    return config
})

//token失效拦截
service.interceptors.response.use(response=>{
    const {warning}=Message
    if(response.data.code===800){
        router.push("/main/login").then(()=>{
            warning("您的权限已失效!请重新登录")
        })
    }else if(response.data.code===801){
        warning("您没有权限访问哦~~")
        router.go(-1)
    }else if(response.data.code===200||response.data.code===300){
        const {data}=response
        const msg=data?.extend.msg||data.msg
        warning(msg)
        return
    }
    return response
})

export default service
